import json
from turtledemo.penrose import f

from 期末项目.teacher.teacher import Teacher


class TeacherManager(object):
    # 创建构造方法并且构造方法里面创建一个tea_list列表
    # 创建一个run方法

    def __init__(self):

        self.tea_list = []

    def run(self):
        while True:
            self.xianshi()
            xz = input("请输入选项：")
            if xz == "1":
                self.tea_add()
            elif xz == "2":
                self.tea_del()
            elif xz == "3":
                self.modify_teacher()
            elif xz == "4":
                self.query_teacher()
            elif xz == "5":
                self.query_all_teachers()
            elif xz == "6":
                self.save_to_file()
            elif xz == "7":
                self.exit_system()
                break
            else:
                print("无效选项，请重新输入")

    def xianshi(self):
        print("1.增加教师功能")
        print("2.删除教师功能")
        print("3.修改教师功能")
        print("4.查询一个教师功能")
        print("5.查询全部教师功能")
        print("6.保存到文件教师功能")
        print("7.退出系统功能")

    def tea_add(self):
        name = input("请输入教师名字：")
        age = input("请输入教师的年龄:")
        gender = input("请输入教师性别:")
        tel = input("请输入教师电话号码:")
        course = input("请输入教师的课程:")
        a = Teacher(name, age, gender, tel, course)
        self.tea_list.append(a)
        print("教师添加成功！")

    # 1.判断列表内部是否有数据
    # 2.如果有数据就键盘输入想要删除的名字
    # 3.遍历列表并判断如果列表内部有对象的name属性值和删除的名字相同那么就删除这个对象
    # 否则就输出没有找到你想要删除的对象
    # 4.遍历输出查看删除之后的列表内容
    def tea_del(self):
        global name
        if len(self.tea_list) == 0:
            print("列表没有数据")
        else:
            name = input("请输入修改对象的名字")
        for i in self.tea_list:
            if i.name == name:
                self.tea_list.remove(i)
            else:
                print("找不到")
            print("操作后的列表数")
            for i in self.tea_list:
                print(i)

        # name = input("请输入要删除的教师姓名：")
        # for teacher in self.tea_list:
        #     if teacher.name == name:
        #         self.tea_list.remove(teacher)
        #         print("教师删除成功！")
        #         return
        # print("教师不存在！")

    def modify_teacher(self):
        if len(self.tea_list) == 0:
            print("列表没数据")
        else:
            name = input("请输入修改名字")
            for i in self.tea_list:
                if i.name == name:
                    i.name = input("请输入名字")
                    i.age = input("请输入年龄")
                    i.tel = input("请输入电话")
                    i.gender = input("请输入修改性别")
                else:
                    print("没找到")
                print("操作之后表的数据")
                for i in self.tea_list:
                    print(i)

        # name = input("请输入要修改的教师姓名：")
        # for teacher in self.tea_list:
        #     if teacher.name == name:
        #         new_name = input("请输入修改后的教师姓名：")
        #         teacher.name = new_name
        #         print("教师修改成功！")
        #         return
        # print("教师不存在！")

    def query_teacher(self):
        if len(self.tea_list) == 0:
            print("列表没有数据")
        else:
            name = input("请输入查询名字")
            for i in self.tea_list:
                if i.name == name:
                    print(i)
            else:
                print("没有找到")
        # name = input("请输入要查询的教师姓名：")
        # for teacher in self.tea_list:
        #     if teacher.name == name:
        #         print("教师存在！")
        #         return
        # print("教师不存在！")

    def query_all_teachers(self):
        if len(self.tea_list) == 0:
            print("列表没有数据")
        else:
            for i in self.tea_list:
                print(i)
        # if len(self.tea_list) > 0:
        #     print("教师列表：")
        #     for teacher in self.tea_list:
        #         print(teacher.name)
        # else:
        #     print("暂无教师信息！")

    def save_to_file(self):
        with open("teacher.data","w",encoding="UTF-8") as f:
            new_list= [i.__dict__ for i in self.tea_list]
            json.dump(new_list ,f, ensure_ascii=False)
    def load(self):
        try:
            f = open("teacher.data","r",encoding="UTF-8")
        except:
            f =open("teacher.data","w",encoding="UTF-8")
        else:
            data = f.read()
            new_list = eval(data) # 把字符串转换为字典
            self.tea_list = [Teacher(i["name"], i["age"],
                                      i["gender"],i["tel"])
                             for i in new_list]
        finally:
            f.close()
        # filename = input("请输入保存文件的路径和名称：")
        # with open(filename, 'w') as file:
        #     for teacher in self.tea_list:
        #         file.write(teacher.name + '')
        #         print("教师信息保存成功！")

    def exit_system(self):
        print("退出系统")

    # 测试
    # manager = TeacherManager()
    # manager.run()
